package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltMatchTypeBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltMatchTypeVo;
import com.ruoyi.zhgd.domain.NAsphaltMatchType;
import com.ruoyi.zhgd.mapper.NAsphaltMatchTypeMapper;
import com.ruoyi.zhgd.service.INAsphaltMatchTypeService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 级配类型Service业务层处理
 *
 * @author tll
 * @date 2023-10-12
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltMatchTypeServiceImpl implements INAsphaltMatchTypeService {

    private final NAsphaltMatchTypeMapper baseMapper;

    /**
     * 查询级配类型
     */
    @Override
    public NAsphaltMatchTypeVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询级配类型列表
     */
    @Override
    public TableDataInfo<NAsphaltMatchTypeVo> queryPageList(NAsphaltMatchTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltMatchType> lqw = buildQueryWrapper(bo);
        Page<NAsphaltMatchTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询级配类型列表
     */
    @Override
    public List<NAsphaltMatchTypeVo> queryList(NAsphaltMatchTypeBo bo) {
        LambdaQueryWrapper<NAsphaltMatchType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltMatchType> buildQueryWrapper(NAsphaltMatchTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltMatchType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), NAsphaltMatchType::getDeptPkid, bo.getDeptPkid());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), NAsphaltMatchType::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NAsphaltMatchType::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getSieveArray()), NAsphaltMatchType::getSieveArray, bo.getSieveArray());
        lqw.eq(bo.getMaxSieveSize() != null, NAsphaltMatchType::getMaxSieveSize, bo.getMaxSieveSize());
        lqw.eq(bo.getNominalMaxSieveSize() != null, NAsphaltMatchType::getNominalMaxSieveSize, bo.getNominalMaxSieveSize());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), NAsphaltMatchType::getInfo, bo.getInfo());
        lqw.eq(bo.getCreator() != null, NAsphaltMatchType::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NAsphaltMatchType::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NAsphaltMatchType::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NAsphaltMatchType::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增级配类型
     */
    @Override
    public Boolean insertByBo(NAsphaltMatchTypeBo bo) {
        NAsphaltMatchType add = BeanUtil.toBean(bo, NAsphaltMatchType.class);
        validEntityBeforeSave(add);
        add.setCreator(LoginHelper.getUserId());
        add.setCreateDate(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改级配类型
     */
    @Override
    public Boolean updateByBo(NAsphaltMatchTypeBo bo) {
        NAsphaltMatchType update = BeanUtil.toBean(bo, NAsphaltMatchType.class);
        validEntityBeforeSave(update);
        update.setUpdater(LoginHelper.getUserId());
        update.setUpdateDate(new Date());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltMatchType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除级配类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据类型获取级配类型
     *
     * @param type 类型
     * @return 级配类型
     */
    @Override
    public NAsphaltMatchType getByType(String type) {
        if(ObjectUtils.isEmpty(type)) {
            return null;
        }
        QueryWrapper<NAsphaltMatchType> queryWrapper = new QueryWrapper<NAsphaltMatchType>();
        queryWrapper.like("type", type);
        List<NAsphaltMatchType> list = baseMapper.selectList(queryWrapper);
        return null == list || 0 == list.size() ? null : list.get(0);
    }

    /**
     * 下拉菜单级配类型
     *
     * @param bo
     * @return
     */
    @Override
    public List<NAsphaltMatchTypeVo> listSelect(NAsphaltMatchTypeBo bo) {
        List<NAsphaltMatchTypeVo> data = baseMapper.listSelect(bo);
        return data;
    }

    /**
     * 获取级配类型列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltMatchTypeVo> page(NAsphaltMatchTypeBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.page(bo, pageQuery.build()));
    }
}
